<?php

/**

 * 行政办公
 * 
 * @access  public 
 * @author  qiaoshoujuan<qiao_sj@jsumt.com.cn> 
 */

namespace Home\Controller;

use Think\Controller;

class ErpController extends BaseController {

    public function _initialize() {
        parent::_initialize();
        $this->title = '公告通知';

        //获取公告分类
        $map['parent_no'] = 'notify';
        $this->notice_code = M('sys_code')->where($map)->order('code_no')->select();
        
        //获取新闻分类
        $map = [];
        $map['parent_no'] = 'news';
        $this->news_code = M('sys_code')->where($map)->order('code_no')->select();
        
        //获取公文分类
        $map = [];
        $map['parent_no'] = 'document';
        $this->document_code = M('sys_code')->where($map)->order('code_no')->select();
        
        //获取用户的辅助角色
        $userinfo = M('user')->field('user_id,user_name,user_priv,user_priv_other')->select();
        
        //获取用户可审批公告通知的用户
        $notice_menu_id = M('backmenu')->where('name like "%公告通知审批%"')->getField('id');
        $privs = M('user_priv')->field('user_priv,priv_name,priv_no')->where('menu_id like "%'.$notice_menu_id.'%"')->select();
        unset($map);
        foreach($userinfo as $k=>$v){
            foreach($privs as $key=>$value){
                if($value['user_priv'] == $v['user_priv']){
                    $notice_users[] = $v;
                }
                if($v['user_priv_other']){
                    $priv_other_arr = explode(',',substr($v['user_priv_other'],0,-1));
                    if(in_array($value['user_priv'],$priv_other_arr)){
                        $notice_users[] = $v;
                    }
                }
            }
        }
        $notice_users = array_unique($notice_users,SORT_REGULAR);
        sort($notice_users);
        $this->notice_users = $notice_users;
        
        //获取用户可审批公文的用户
        $document_menu_id = M('backmenu')->where('name like "%公文发布审批%"')->getField('id');
        $privs = M('user_priv')->field('user_priv,priv_name,priv_no')->where('menu_id like "%'.$document_menu_id.'%"')->select();
        unset($map);
        foreach($userinfo as $k=>$v){
            foreach($privs as $key=>$value){
                if($value['user_priv'] == $v['user_priv']){
                    $document_users[] = $v;
                }
                if($v['user_priv_other']){
                    $priv_other_arr = explode(',',substr($v['user_priv_other'],0,-1));
                    if(in_array($value['user_priv'],$priv_other_arr)){
                        $document_users[] = $v;
                    }
                }
            }
        }
        $document_users = array_unique($document_users,SORT_REGULAR);
        sort($document_users);
        $this->document_users = $document_users;
    }
    
    //公告通知管理
    public function noticeManage(){
        if(IS_AJAX){
            $data = I('get.');
            if($data['status'] == '终止'){
                $ndata['end_date'] = time();
                $ndata['publish'] = 4;
            }elseif($data['status'] == '生效'){
                $ndata['end_date'] = '0';
                $ndata['publish'] = 1;
            }
            $res = M('notify')->where('notify_id = '.I('notify_id'))->save($ndata);
            if($res !== false){
                $return['status'] = 1; 
            }else{
                $return['status'] = 0; 
            }
            $this->ajaxReturn($return);
        }
        $map = [];
        $p = I('get.p');
        $limit = $this->limit;
        if(I('type_id')){
            $this->type_id = I('type_id');
            $map['type_id'] = array('eq',I('type_id'));
        }
        $count = M('notify')->where($map)->count();
        $notify = $this->getNotice($map,$p,$limit);
        $this->notify = $notify;
        $this->count = $count;
        $this->page = $this->pageLists($count, $limit);
        $this->display();
    }

    //公告通知审批
    public function noticeApprove(){
        if(IS_AJAX){
            $data = I('post.');
            $data['begin_date'] = $data['begin_date']?strtotime($data['begin_date']):''; 
            $data['end_date'] = $data['end_date']?strtotime($data['end_date']):'';
            $data['audit_date'] = date('Y-m-d');
            $map['notify_id'] = $data['notify_id'];
            $res = M('notify')->where($map)->save($data);
            $return = $res !== false ? true : false;
            $this->ajaxReturn($return);
        }
        //获取审批人要审批的公告
        if(I('type_id')){
            $this->type_id = I('type_id');
            $map['type_id'] = array('eq',I('type_id'));
        }
        $map['auditer'] = array('like','%'.session('user_id').'%');
        $map['publish'] = array('eq','2'); 
        $notify = M('notify')->field('notify_id,from_dept,from_id,to_id,subject,content,send_time,begin_date,end_date,priv_id,user_id,type_id,readers,publish')->where($map)->select();
        foreach($notify as $k=>$v){
            $deptName = [];
            $notify[$k]['begin_date'] = date("Y-m-d",$v['begin_date']);
            $notify[$k]['end_date'] = $v['end_date']?date("Y-m-d",$v['end_date']):'';
            
            if($v['to_id'] == 'ALL_DEPT'){
                $notify[$k]['to_id'] = '全体部门';
            }else{
                $deptNameArr = M('department')->field('dept_name')->where('dept_id in('.substr($v['to_id'],0,-1).')')->select();
                foreach($deptNameArr as $key=>$value){
                    $deptName[] = $value['dept_name'];
                }
                $notify[$k]['to_id'] = implode(',',$deptName);
            }
            if($v['priv_id']){//发布角色范围
                $userPrivArr = M('user_priv')->field('priv_name')->where('user_priv in('.substr($v['priv_id'],0,-1).')')->select();
                foreach($userPrivArr as $key=>$value){
                    $privName[] = $value['priv_name'];
                }
                $notify[$k]['priv_id'] = implode(',',$privName);
            }
            if($v['user_id']){//发布人员范围
                $userNameArr = explode(',',substr($v['user_id'],0,-1));
                foreach($userNameArr as $u_value){
                    $userName[] = M('user')->where('user_id = "'.$u_value.'"')->getField('user_name');
                }
                $notify[$k]['user_id'] = implode(',',$userName);
            }
            if(strstr($v['readers'],session('user_id'))){
                $notify[$k]['is_read'] = 2;//已读
            }else{
                $notify[$k]['is_read'] = 1;//未读
            }
        }
        $this->count = count($notify);
        $this->notify = $notify;
        $this->display();
    }

    //新建公告
    public function saveNotice() {
        if (IS_AJAX) {
            $data = I('post.');
            $data['begin_date'] = $data['begin_date'] ? strtotime($data['begin_date']) : time();
            $data['end_date'] = $data['end_date'] ? strtotime($data['end_date']) : 0;
            $data['to_id'] = $data['dept_ids'] ? implode(',', $data['dept_ids']).',' : 'ALL_DEPT';
            $data['priv_id'] = $data['priv_id'] ? implode(',', $data['priv_id']).',':'';
            $data['user_id'] = $data['user_id'] ? implode(',', $data['user_id']).',':'';
            $data['publish'] = 2;
            if ($data['notify_id']) {
                $row = M('notify')->where('notify_id=' . $data['notify_id'])->find();
                if ($data['uploads_url'] && $data['uploads_url'] != $row['uploads_url']) {
                    @unlink('.' . $row['uploads_url']);
                } else {
                    $data['uploads_url'] = $row['uploads_url'];
                }
                $data['last_editor'] = session('user_id');
                $data['last_edit_time'] = date('Y-m-d H:i:s');
                $res = M('notify')->where('notify_id=' . $data['notify_id'])->save($data);
            } else {
                $data['from_dept'] = session('dept_id');
                $data['from_id'] = session('user_id');
                $data['print'] = 1;
                $data['download'] = 1;
                $data['send_time'] = date('Y-m-d H:i:s');
                $res = M('notify')->add($data);
            }
            if ($res !== false) {
                $return['info'] = '提交成功';
                $return['url'] = U('noticeManage', array('type_id' => I('type_id')));
            } else {
                $return['info'] = '提交失败';
            }
            $this->ajaxReturn($return);
        }

        if (I('notify_id')) {
            $info = M('notify')->where('notify_id=' . I('notify_id'))->find();
            if ($info['to_id'] != 'ALL_DEPT') {
                $this->to_ids = explode(',', substr($info['to_id'],0,-1));
            }
            if ($info['priv_id']) {
                $this->priv_ids = explode(',', substr($info['priv_id'],0,-1));
            }
            if ($info['user_id']) {
                $this->user_ids = explode(',', substr($info['user_id'],0,-1));
            }
            //获取pc端上传的附件
            if($info['attachment_name']){
                $pcNames = downloadFile($info['attachment_id'],$info['attachment_name'],'notify');
            }
            
            $info['begin_date'] = $info['begin_date']?date("Y-m-d",$info['begin_date']):'';
            $info['end_date'] = $info['end_date']?date("Y-m-d",$info['end_date']):'';
            $this->notify_id = I('notify_id');
            $this->type_id = I('type_id');
            $this->info = $info;
//            echo '<pre>',print_r($info);die;
        }

        $this->display();
    }
    
    //获取公告通知数据
    public function getNotice($map,$p,$limit){
        $start = $limit * ($p - 1);
        $notify = M('notify')->field('notify_id,from_dept,from_id,to_id,subject,content,send_time,begin_date,end_date,priv_id,user_id,type_id,readers,publish')->where($map)->order('notify_id desc')->page($p,$limit)->select();
        foreach($notify as $k=>$v){
            $deptName = [];
            $notify[$k]['begin_date'] = date("Y-m-d",$v['begin_date']);
            $notify[$k]['end_date'] = $v['end_date']?date("Y-m-d",$v['end_date']):'';
            if($v['to_id'] == 'ALL_DEPT'){
                $notify[$k]['to_id'] = '全体部门';
            }else{
                $deptNameArr = M('department')->field('dept_name')->where('dept_id in('.substr($v['to_id'],0,-1).')')->select();
                foreach($deptNameArr as $key=>$value){
                    $deptName[] = $value['dept_name'];
                }
                $notify[$k]['to_id'] = implode(',',$deptName);
            }
            if($v['priv_id']){//发布角色范围
                $userPrivArr = M('user_priv')->field('priv_name')->where('user_priv in('.substr($v['priv_id'],0,-1).')')->select();
                foreach($userPrivArr as $key=>$value){
                    $privName[] = $value['priv_name'];
                }
                $notify[$k]['priv_id'] = implode(',',$privName);
            }
            if($v['user_id']){//发布人员范围
                $userNameArr = explode(',',substr($v['user_id'],0,-1));
                foreach($userNameArr as $u_value){
                    $userName[] = M('user')->where('user_id = "'.$u_value.'"')->getField('user_name');
                }
                $notify[$k]['user_id'] = implode(',',$userName);
            }
            if(strstr($v['readers'],session('user_id'))){
                $notify[$k]['is_read'] = 2;//已读
            }else{
                $notify[$k]['is_read'] = 1;//未读
            }
        }
        return $notify;
    }
    
    //删除公告
    public function delNotice() {
        $notify_id = I('notify_id');
        $notify = M('notify')->where('notify_id=' . $notify_id)->find();
        $res = M('notify')->where('notify_id=' . $notify_id)->delete();
        //删除附件
        if ($notify['uploads_url']) {
            $urls = explode(';', rtrim($notify['uploads_url'], ';'));
            foreach ($urls as $url) {
                @unlink('.' . $url);
            }
        }
        if ($res !== false) {
            $return['info'] = '删除成功';
        } else {
            $return['info'] = '删除失败';
        }
        $this->ajaxReturn($return);
    }
    
    //新闻管理
    public function newsManage(){
        if(IS_AJAX){
            $data = I('get.');
            if($data['status'] == '终止'){
                $ndata['end_date'] = time();
                $ndata['publish'] = 4;
            }elseif($data['status'] == '生效'){
                $ndata['end_date'] = '0';
                $ndata['publish'] = 1;
            }
            $res = M('notify')->where('notify_id = '.I('notify_id'))->save($ndata);
            if($res !== false){
                $return['status'] = 1; 
            }else{
                $return['status'] = 0; 
            }
            $this->ajaxReturn($return);
        }
        $map = [];
        $p = I('get.p');
        $limit = $this->limit;
        if(I('type_id')){
            $this->type_id = I('type_id');
            $map['type_id'] = array('eq',I('type_id'));
        }
        $count = M('news')->where($map)->count();
        $news = M('news')->field('news_id,provider,to_id,subject,content,news_time,priv_id,user_id,type_id,readers,click_count,keyword,last_editor,last_edit_time,anonymity_yn')->where($map)->order('news_id desc')->page($p,$limit)->select();
        foreach($news as $k=>$v){
            $deptName = [];
            if($v['to_id'] == 'ALL_DEPT'){
                $news[$k]['to_id'] = '全体部门';
            }else{
                $deptNameArr = M('department')->field('dept_name')->where('dept_id in('.substr($v['to_id'],0,-1).')')->select();
                foreach($deptNameArr as $key=>$value){
                    $deptName[] = $value['dept_name'];
                }
                $news[$k]['to_id'] = implode(',',$deptName);
            }
            if($v['priv_id']){//发布角色范围
                $userPrivArr = M('user_priv')->field('priv_name')->where('user_priv in('.substr($v['priv_id'],0,-1).')')->select();
                foreach($userPrivArr as $key=>$value){
                    $privName[] = $value['priv_name'];
                }
                $news[$k]['priv_id'] = implode(',',$privName);
            }
            if($v['user_id']){//发布人员范围
                $userNameArr = explode(',',substr($v['user_id'],0,-1));
                foreach($userNameArr as $u_value){
                    $userName[] = M('user')->where('user_id = "'.$u_value.'"')->getField('user_name');
                }
                $news[$k]['user_id'] = implode(',',$userName);
            }
            if(strstr($v['readers'],session('user_id'))){
                $news[$k]['is_read'] = 2;//已读
            }else{
                $news[$k]['is_read'] = 1;//未读
            }
            $news[$k]['comment_count'] = M('news_comment')->where('news_id = '.$v['news_id'])->count();
        }
        $this->news = $news;
        $this->count = $count;
        $this->page = $this->pageLists($count, $limit);
        $this->display();
    }
    
    //新建新闻
    public function saveNews() {
        if (IS_AJAX) {
            $data = I('post.');
            $data['news_time'] = date('Y-m-d H:i:s');
            $data['provider'] = session('user_id');
            $data['to_id'] = $data['dept_ids'] ? implode(',', $data['dept_ids']).',' : 'ALL_DEPT';
            $data['priv_id'] = $data['priv_id'] ? implode(',', $data['priv_id']).',':'';
            $data['user_id'] = $data['user_id'] ? implode(',', $data['user_id']).',':'';
            if ($data['news_id']) {
                $row = M('news')->where('news_id=' . $data['news_id'])->find();
                if ($data['uploads_url'] && $data['uploads_url'] != $row['uploads_url']) {
                    @unlink('.' . $row['uploads_url']);
                } else {
                    $data['uploads_url'] = $row['uploads_url'];
                }
                $data['last_editor'] = session('user_id');
                $data['last_edit_time'] = date('Y-m-d H:i:s');
                $res = M('news')->where('news_id=' . $data['news_id'])->save($data);
            } else {
                $res = M('news')->add($data);
            }
            if ($res !== false) {
                $return['info'] = '提交成功';
                $return['url'] = U('newsManage', array('type_id' => I('type_id')));
            } else {
                $return['info'] = '提交失败';
            }
            $this->ajaxReturn($return);
        }

        if (I('news_id')) {
            $info = M('news')->where('news_id=' . I('news_id'))->find();
            if ($info['to_id'] != 'ALL_DEPT') {
                $this->to_ids = explode(',', substr($info['to_id'],0,-1));
            }
            if ($info['priv_id']) {
                $this->priv_ids = explode(',', substr($info['priv_id'],0,-1));
            }
            if ($info['user_id']) {
                $this->user_ids = explode(',', substr($info['user_id'],0,-1));
            }
            //获取pc端上传的附件
            if($info['attachment_name']){
                $pcNames = downloadFile($info['attachment_id'],$info['attachment_name'],'news');
            }
            $this->news_id = I('news_id');
            $this->type_id = I('type_id');
            $this->info = $info;
        }

        $this->display();
    }
    
    //删除新闻
    public function delNews() {
        $news_id = I('news_id');
        $news = M('news')->where('news_id=' . $news_id)->find();
        $res = M('news')->where('news_id=' . $news_id)->delete();
        $r = M('news_comment')->where('news_id = '.$news_id)->delete();
        //删除附件
        if ($news['uploads_url']) {
            $urls = explode(';', rtrim($news['uploads_url'], ';'));
            foreach ($urls as $url) {
                @unlink('.' . $url);
            }
        }
        if ($res !== false) {
            $return['info'] = '删除成功';
        } else {
            $return['info'] = '删除失败';
        }
        $this->ajaxReturn($return);
    }
    
    //上传文件
    public function upload() {
        $this->upfile('./Public/Uploads/'.I('fileType').'/');
    }
    
    //公文发布管理
    public function documentManage(){
        if(IS_AJAX){
            $data = I('get.');
            if($data['status'] == '终止'){
                $ndata['end_date'] = date('Y-m-d');
                $ndata['publish'] = 4;
            }elseif($data['status'] == '生效'){
                $ndata['end_date'] = '';
                $ndata['publish'] = 1;
            }
            $res = M('document')->where('document_id = '.I('document_id'))->save($ndata);
            if($res !== false){
                $return['status'] = 1; 
            }else{
                $return['status'] = 0; 
            }
            $this->ajaxReturn($return);
        }
        $map = [];
        $p = I('get.p');
        $limit = $this->limit;
        if(I('type_id')){
            $this->type_id = I('type_id');
            $map['type_id'] = array('eq',I('type_id'));
        }
        $count = M('document')->where($map)->count();
        $document = $this->getDocument($map,$p,$limit);
//        echo '<pre>',print_r($document);die;
        $this->document = $document;
        $this->count = $count;
        $this->page = $this->pageLists($count, $limit);
        $this->display();
    }
    
    //新建公文制度
    public function saveDocument() {
        if (IS_AJAX) {
            $data = I('post.');
            $data['to_id'] = $data['dept_ids'] ? implode(',', $data['dept_ids']).',' : 'ALL_DEPT';
            $data['priv_id'] = $data['priv_id'] ? implode(',', $data['priv_id']).',':'';
            $data['user_id'] = $data['user_id'] ? implode(',', $data['user_id']).',':'';
            $data['publish'] = 2;
            if ($data['document_id']) {
                $row = M('document')->where('document_id=' . $data['document_id'])->find();
                if ($data['uploads_url'] && $data['uploads_url'] != $row['uploads_url']) {
                    @unlink('.' . $row['uploads_url']);
                } else {
                    $data['uploads_url'] = $row['uploads_url'];
                }
                $data['last_editor'] = session('user_id');
                $data['last_edit_time'] = date('Y-m-d H:i:s');
                $res = M('document')->where('document_id=' . $data['document_id'])->save($data);
            } else {
                $data['from_dept'] = session('dept_id');
                $data['from_id'] = session('user_id');
                $data['print'] = 1;
                $data['download'] = 1;
                $data['send_time'] = date('Y-m-d H:i:s');
                $res = M('document')->add($data);
            }
            if ($res !== false) {
                $return['info'] = '提交成功';
                $return['url'] = U('documentManage', array('type_id' => I('type_id')));
            } else {
                $return['info'] = '提交失败';
            }
            $this->ajaxReturn($return);
        }

        if (I('document_id')) {
            $info = M('document')->where('document_id=' . I('document_id'))->find();
            if ($info['to_id'] != 'ALL_DEPT') {
                $this->to_ids = explode(',', substr($info['to_id'],0,-1));
            }
            if ($info['priv_id']) {
                $this->priv_ids = explode(',', substr($info['priv_id'],0,-1));
            }
            if ($info['user_id']) {
                $this->user_ids = explode(',', substr($info['user_id'],0,-1));
            }
            //获取pc端上传的附件
            if($info['attachment_name']){
                $pcNames = downloadFile($info['attachment_id'],$info['attachment_name'],'document');
            }
            $this->document_id = I('document_id');
            $this->type_id = I('type_id');
            $this->info = $info;
        }

        $this->display();
    }
    
    //公文发布审批
    public function documentApprove(){
        if(IS_AJAX){
            $data = I('post.');
            $data['audit_date'] = date('Y-m-d');
            $map['document_id'] = $data['document_id'];
            $res = M('document')->where($map)->save($data);
            $return = $res !== false ? true : false;
            $this->ajaxReturn($return);
        }
        $map = [];
        $p = I('get.p');
        $limit = $this->limit;
        //获取审批人要审批的公告
        if(I('type_id')){
            $this->type_id = I('type_id');
            $map['type_id'] = array('eq',I('type_id'));
        }
        $map['auditer'] = array('like','%'.session('user_id').'%');
        $map['publish'] = array('eq','2'); 
        $count = M('document')->where($map)->count();
        $document = $this->getDocument($map,$p,$limit);
        $this->count = count($document);
        $this->document = $document;
        $this->display();
    }
    
    //删除公文
    public function delDocument() {
        $document_id = I('document_id');
        $document = M('document')->where('document_id=' . $document_id)->find();
        $res = M('document')->where('document_id=' . $document_id)->delete();
        //删除附件
        if ($document['uploads_url']) {
            $urls = explode(';', rtrim($document['uploads_url'], ';'));
            foreach ($urls as $url) {
                @unlink('.' . $url);
            }
        }
        if ($res !== false) {
            $return['info'] = '删除成功';
        } else {
            $return['info'] = '删除失败';
        }
        $this->ajaxReturn($return);
    }
    
    //获取公文数据
    public function getDocument($map,$p,$limit){
        $start = $limit * ($p - 1);
        $document = M('document')->field('document_id,from_dept,from_id,to_id,subject,content,send_time,begin_date,end_date,priv_id,user_id,type_id,readers,publish')->where($map)->order('document_id desc')->page($p,$limit)->select();
//        echo M()->getLastSql();die;
        foreach($document as $k=>$v){
            $deptName = [];
            if($v['to_id'] == 'ALL_DEPT'){
                $notify[$k]['to_id'] = '全体部门';
            }else{
                $deptNameArr = M('department')->field('dept_name')->where('dept_id in('.substr($v['to_id'],0,-1).')')->select();
                foreach($deptNameArr as $key=>$value){
                    $deptName[] = $value['dept_name'];
                }
                $document[$k]['to_id'] = implode(',',$deptName);
            }
            if($v['priv_id']){//发布角色范围
                $userPrivArr = M('user_priv')->field('priv_name')->where('user_priv in('.substr($v['priv_id'],0,-1).')')->select();
                foreach($userPrivArr as $key=>$value){
                    $privName[] = $value['priv_name'];
                }
                $document[$k]['priv_id'] = implode(',',$privName);
            }
            if($v['user_id']){//发布人员范围
                $userNameArr = explode(',',substr($v['user_id'],0,-1));
                foreach($userNameArr as $u_value){
                    $userName[] = M('user')->where('user_id = "'.$u_value.'"')->getField('user_name');
                }
                $document[$k]['user_id'] = implode(',',$userName);
            }
            if(strstr($v['readers'],session('user_id'))){
                $document[$k]['is_read'] = 2;//已读
            }else{
                $document[$k]['is_read'] = 1;//未读
            }
        }
        return $document;
    }
    
    //切换类别获取数据
    public function changeType(){
        $p = I('get.p');
        $limit = 10;
        $con = I('get.');
        if($con['type_id']){//切换公文类别，获取对应数据
            $map['type_id'] = array('eq',$con['type_id']);
        }
        if($con['model'] == 'documentApprove'){
            $map['auditer'] = array('like','%'.session('user_id').'%');
            $map['publish'] = array('eq','2'); 
        }
        $count = M('document')->where($map)->count();
        if($con['type'] == 'document'){
            $data = $this->getDocument($map,$p,$limit);
        }else{
            $data = $this->getNotice();
        }
        foreach($data as $k=>$v){
            $data[$k]['from_id'] = getChineseName($v['from_id']);
            $data[$k]['type_id'] = getDocumentTypeName($v['type_id']);
        }
        $return['data'] = $data;
        $return['model'] = $con['model'];
        $return['count'] = $count;
        $page = new \Think\AjaxPage($count, $limit);
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $page->setConfig('first', '首页');
        $page->setConfig('last', '尾页');
        $page->rollPage = 5;
        $return['page'] = $page->pageList_ajax();
        $this->ajaxReturn($return);
    }
    
    //批量删除
    public function delMore(){
        $ids = I('ids');
        if(I('type') == 'document'){
            $data = M('document')->field('document_id,uploads_url')->where('document_id in('.$ids.')')->select();
            $res = M('document')->where('document_id in('.$ids.')')->delete();
        }elseif(I('type') == 'notify'){
            $data = M('notify')->field('notify_id,uploads_url')->where('notify_id in('.$ids.')')->select();
            $res = M('notify')->where('notify_id in('.$ids.')')->delete();
        }elseif(I('type') == 'news'){
            $data = M('news')->field('news_id,uploads_url')->where('news_id in('.$ids.')')->select();
            $res = M('news')->where('news_id in('.$ids.')')->delete();
        }elseif(I('type') == 'address'){
            $res = M('address')->where('add_id in('.$ids.')')->delete();
        }
        
        //同时删除附件
        if($data){
            foreach($data as $v){
                if($v['uploads_url']){
                    $urls = explode(';', rtrim($v['uploads_url'], ';'));
                    foreach ($urls as $url) {
                        @unlink('.' . $url);
                    }
                }
            }
        }
        
        if ($res !== false) {
            $return['status'] = 1;
            $return['info'] = '删除成功';
        } else {
            $return['status'] = 0;
            $return['info'] = '删除失败';
        }
        $this->ajaxReturn($return);
    }
    
    //组织机构信息
    public function organize(){
        //单位信息
        $unit = M('unit')->find();
        $unit['content'] = html_entity_decode($unit['content'], ENT_QUOTES, 'UTF-8');
        
        //部门信息
        $department = M('department')->field('dept_id,dept_name,tel_no,fax_no,manager,assistant_id,leader1,leader2,dept_func')->select();
        foreach($department as $k=>$v){
            if($v['manager']){
                $department[$k]['manager_name'] = explode(',',substr($v['manager'],0,-1));
            }
            if($v['assistant_id']){
                $department[$k]['assistant_name'] = explode(',',substr($v['assistant_id'],0,-1));
            }
            if($v['leader1']){
                $department[$k]['leader1_name'] = explode(',',substr($v['leader1'],0,-1));
            }
            if($v['leader2']){
                $department[$k]['leader2_name'] = explode(',',substr($v['leader2'],0,-1));
            }
//            $department[$k]['dept_user'] = M('user')->field('user_id,user_name')->where('DEPT_ID = '.$v['dept_id'])->select();
        }
        $this->unit = $unit;
        $this->department = $department;
        $this->display();
    }
    
    //获取部门人员
    public function getDeptUser(){
        $map['DEPT_ID'] = I('dept_id');
        $deptUser = M('user')->field('user_id,user_name,dept_id,user_priv,user_priv_other,online,sex,mobil_no,email')->where($map)->select();
        foreach($deptUser as $k=>$v){
            $deptUser[$k]['user_priv'] = getPrivName($v['user_priv']);
            if($v['user_priv_other']){
                $priv_other = '';
                $privs = explode(',',substr($v['user_priv_other'],0,-1));
                foreach($privs as $value){
                    $priv_other .= getPrivName($value).',';
                }
                $deptUser[$k]['user_priv_other'] = $priv_other;
            }
            $deptUser[$k]['sex'] = $v['sex'] == 1?'女':'男';
        }
        if($deptUser){
            $return['status'] = 1;
            $return['data'] = $deptUser;
        }else{
            $return['status'] = 0;
        }
        $this->ajaxReturn($return);
    }
    
    //用户查询
    public function searchUser(){
        $data = I('post.');
        $map = [];
        foreach($data as $key=>$value){
            if($value){
                $map[$key] = array('like','%'.$value.'%');
            }
        }
        
        $users = M('user')->field('user_id,user_name,dept_id,user_priv,user_priv_other,online,sex,mobil_no,email')->where($map)->select();
        foreach($users as $k=>$v){
            $users[$k]['user_priv'] = getPrivName($v['user_priv']);
            if($v['user_priv_other']){
                $priv_other = '';
                $privs = explode(',',substr($v['user_priv_other'],0,-1));
                foreach($privs as $value){
                    $priv_other .= getPrivName($value).',';
                }
                $users[$k]['user_priv_other'] = $priv_other;
            }
            $users[$k]['sex'] = $v['sex'] == 1?'女':'男';
        }
        if($users){
            $return['status'] = 1;
            $return['data'] = $users;
        }else{
            $return['status'] = 0;
        }
        $this->ajaxReturn($return);
    }
    
    //通讯录
    public function address(){
        $p = I('get.p');
        $limit = $this->limit;

        //联系人
        $map['is_delete'] = 0;

        if (isset($_GET['user_name']) && I('user_name')) {
            $ids = M('user')->where('user_name like "%' . I('user_name') . '%"')->getField('user_id', true);
            $this->name = I('user_name');
        }
        
        if(isset($_GET['group_id']) && I('group_id')){
            $gmap['user_id'] = session('user_id');
            $gmap['group_id'] = I('group_id');
            $addids = M('address_group_user')->where($gmap)->getField('add_id',true);
            if(!is_array($addids)){
                $addids[] = $addids;
        }
            $map['id'] = array('in',$addids);
            $this->groupId = I('group_id');
        }

        if (isset($_GET['dept_id']) && I('dept_id')) {
            $ids = M('user')->where('dept_id = ' . I('dept_id'))->getField('user_id', true);
            $this->deptid = I('dept_id');
        }
        if (!empty($ids)) {
            if (!is_array($ids)) {
                $ids[] = $ids;
            }
            $map['user_id'] = array('in', $ids);
        }

        $count = M('address')->where($map)->count();
        $users = M('address')->where($map)->page($p, $limit)->select();
        foreach ($users as $k => $v) {
            $users[$k]['info'] = M('user')->where('user_id = "' . $v['user_id'] . '"')->find();
        }
        $this->count = $count;
        $this->page = $this->pageLists($count, $limit);
        $this->myGroup = $this->getMyGroup();
        $this->users = $users;

        //获取所有部门
        $this->depts = M('department')->order('dept_no')->select();

        $this->display();
    }
    
    //获取我的分组
    private function getMyGroup(){
        $myGroup = M('address_group')->where('user_id = "' . session('user_id') . '"')->select();
        return $myGroup;
    }


    //新建分组
    public function saveGroup(){
        $data = I('post.');
        $data['user_id'] = session('user_id');
        if($data['group_name']){
        $res = M('address_group')->add($data);
        }
        if(!empty($data['name'])){
            for($i=0; $i<count($data['name']); $i++){
                $sdata['group_name'] = $data['name'][$i];
                $sdata['id'] = $data['id'][$i];
                $res = M('address_group')->save($sdata);
            }
        }
        if($res !== false){
            $return = ['info'=>'编辑成功','icon'=>6,'parent'=>1,'status'=>1];
        }else{
            $return = ['info'=>'编辑失败','icon'=>5,'status'=>0];
        }
        $this->ajaxReturn($return);
    }
    
    //联系人详情
    public function showAddress(){
        if(IS_AJAX){
            $data['add_id'] = I('post.id');
            $data['group_id'] = I('post.group_id');
            $data['user_id'] = session('user_id');
            $map['add_id'] = I('post.id');
            $map['user_id'] = session('user_id');
            $row = M('address_group_user')->where($map)->count();
            if(!$row){
                //增加
                $res = M('address_group_user')->add($data);
            }else{
                $res = M('address_group_user')->where($map)->save($data);
            }
            
            if($res !== FALSE){
                $return = ['info'=>'分组成功','icon'=>6,'reload'=>1,'status'=>1];
            }else{
                $return = ['info'=>'分组失败','icon'=>5,'status'=>0];
            }
            $this->ajaxReturn($return);
        }
        $data = M('address')->find(I('id'));
        $data['info'] = M('user')->where('user_id="'.$data['user_id'].'"')->find();
        $data['group'] = M('address_group_user')->where('add_id='.I('id') . ' and user_id = "' . session('user_id') . '"')->getField('group_id');
//        print_r($data);
        $this->data = $data;
        $this->groups = $this->getMyGroup();
        $this->display();
    }
    
    }
